package com.example.demo.controller;

//package com.example.demo.controller;

import com.example.demo.entity.DemoEntity;
import com.example.demo.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/demo")
public class DemoController {
    @Autowired
    private DemoService demoService;

    @GetMapping
    public List<DemoEntity> getAll() {
        return demoService.getAll();
    }

    @GetMapping("/{id}")
    public ResponseEntity<DemoEntity> getById(@PathVariable Long id) {
        return demoService.getById(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @PostMapping
    public DemoEntity create(@RequestBody DemoEntity entity) {
        return demoService.create(entity);
    }

    @PutMapping("/{id}")
    public ResponseEntity<DemoEntity> update(@PathVariable Long id, @RequestBody DemoEntity entityDetails) {
        return ResponseEntity.ok(demoService.update(id, entityDetails));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable Long id) {
        demoService.delete(id);
        return ResponseEntity.noContent().build();
    }
}

